package dados;

/**
 * 
 * @author Daniel
 *
 */
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class BancoDeDadosSPC implements IBancoDeDadosSPC {
	
	Conexao conn = new Conexao();
	
	public boolean inserirDevedor(String cpf, float valor){
		
		try{
			Connection conexao = conn.Conectar();
			Statement stm = conexao.createStatement();
			
			String sql = "INSERT INTO Devedor(cpf,valor)VALUES('"+cpf+"','"+valor+"')";
			stm.executeUpdate(sql);
			
			System.out.println("Devedor Inserido com Sucesso!");
			return true;
			
		}catch(SQLException ex){
			System.out.println(ex);
			return false;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		
	}
	
	public boolean removerDevedor(String cpf){
		
		try{
			Connection conexao = conn.Conectar();
			Statement stm = conexao.createStatement();
			
			String sql = "DELETE FROM Devedor WHERE cpf = '"+cpf+"'";
			stm.executeUpdate(sql);
			
			System.out.println("Devedor Removido com Sucesso!");
			return true;
		} catch(SQLException ex){
			System.out.println(ex);
			return false;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}
	
	public boolean eDevedor(String cpf){
		boolean retorno = false;
		try
		{
			Connection conexao = conn.Conectar();
			Statement stm = conexao.createStatement();
			String sql = "SELECT cpf FROM Devedor WHERE Cpf='"+cpf+"'";
			
			ResultSet rs = stm.executeQuery(sql);
			if (rs.next()){
				 retorno = true;						
			}
			
		} catch(SQLException ex){
			System.out.println(ex);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return retorno;
	}
	
	public String getDevedores(){
		String devedor = null;
		try
		{
			Connection conexao = conn.Conectar();
			Statement stm = conexao.createStatement();
			String sql = "SELECT cpf, valor FROM Devedor";
			
			ResultSet rs = stm.executeQuery(sql);
			while(rs.next())
			{
				 devedor += rs.getString("cpf");	
				 devedor += ",";
				 devedor += rs.getString("valor");
				 devedor += "-";								
			}			
			
		} catch(SQLException ex){
			System.out.println(ex);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return devedor;
	}	
}
